package com.example.hrdemo.console.controller;

import com.example.hrdemo.console.domain.UserAllVo;
import com.example.hrdemo.console.domain.UserAllPageVo;
import com.example.hrdemo.console.domain.UserInfoVo;
import com.example.hrdemo.module.entity.User;
import com.example.hrdemo.module.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class UserConsoleController {

    @Autowired
    private UserService userService;

    //员工列表--分页
    //http://localhost:8081/user/allPage?page=5
    @RequestMapping("/user/allPage")
    public UserAllPageVo userAllVoPage(@RequestParam(name ="page") Integer page,@RequestParam(required = false,name ="keyword") String keyword){
        UserAllPageVo userAllPage = new UserAllPageVo();
        //设置一行显示5条
        int pageSize = 5;
        //获取总条数
        int total = userService.getUserCount();
        //计算页数
        int pages;
        if (total%pageSize == 0){
            pages = total/pageSize;
        } else {
            pages = total/pageSize+1;
        }
        //如果页数大于计算，则显示错误，为空
        if (page>pages){
            userAllPage.setTotal(total);
            userAllPage.setPageSize(pageSize);
            return userAllPage;
        } else {
            //如果输入一页五条时，相应的页码，则正常显示
            List<User> userList = userService.getAllPage(keyword,page,pageSize);
            List<UserAllVo> userAllVo = new ArrayList<>();
            for (User u : userList) {
                UserAllVo userAll = new UserAllVo();
                userAll.setId(u.getId());
                //多图显示第一个
                String pictures = u.getPicturesUrl();
                String[] picture = pictures.split("\\$");
                String picture1 = picture[0].trim();
                userAll.setPicturesUrl(picture1);
                userAll.setUserName(u.getUserName());
                userAll.setUserWorkNum(u.getUserWorkNum());
                userAll.setJobName(u.getJobName());
                userAllVo.add(userAll);
            }
            userAllPage.setTotal(total);
            userAllPage.setPageSize(pageSize);
            userAllPage.setUserAllList(userAllVo);
            return userAllPage;
        }
    }

    @RequestMapping("/user/info")
    public UserInfoVo userInfo(@RequestParam(name = "userId") BigInteger userId){
        User user = userService.getUserInfo(userId);
        if (user == null){
            return null;
        } else {
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setId(user.getId());
            userInfoVo.setUserName(user.getUserName());
            userInfoVo.setUserGender(user.getUserGender());
            userInfoVo.setUserCardNum(user.getUserCardNum());
            userInfoVo.setBirthday(user.getBirthday());
            userInfoVo.setEducation(user.getEducation());
            userInfoVo.setBirthplace(user.getBirthplace());
            userInfoVo.setPhone(user.getPhone());
            userInfoVo.setUserWorkNum(user.getUserWorkNum());
            userInfoVo.setJobName(user.getJobName());
            userInfoVo.setWages(user.getWages());
            userInfoVo.setWorkDay(user.getWorkDay());
            userInfoVo.setFormalWorkDay(user.getFormalWorkDay());
            userInfoVo.setWorkAge(user.getWorkAge());
            //显示状态
            int status = user.getUserStatus();
            Map<Integer,String> statusMap = new HashMap<>();
            statusMap.put(1,"试用");
            statusMap.put(2,"正式");
            statusMap.put(3,"离职");
            String s = statusMap.get(status);
            userInfoVo.setUserStatus(s);
            //多图按照列表显示出来
            String pictures = user.getPicturesUrl();
            List<String> pictureList = Arrays.stream(pictures.split("\\$")).collect(Collectors.toList());
            userInfoVo.setPicturesUrl(pictureList);
            //显示创建时间和修改时间，做格式转换 2025-03-09 21:34:54
            int createTime = user.getCreateTime();
            long createTimeDate = createTime * 1000L;
            String createDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(createTimeDate));
            userInfoVo.setCreateTime(createDate);
            int updateTime = user.getUpdateTime();
            long updateTimeDate = updateTime * 1000L;
            String updateDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(updateTimeDate));
            userInfoVo.setUpdateTime(updateDate);
            return userInfoVo;
        }
    }



    //创建一个员工
    @RequestMapping("/user/create")
    public String userCreate(@RequestParam(name ="userName") String userName,@RequestParam(name ="userGender") Integer userGender ,
                             @RequestParam(name ="userCardNum") String userCardNum,@RequestParam(name ="birthday") String birthday,
                             @RequestParam(name ="education") String education,@RequestParam(name ="birthplace") String birthplace,
                             @RequestParam(name ="phone") String phone,@RequestParam(name ="userWorkNum") String userWorkNum,
                             @RequestParam(name ="jobName") String jobName,@RequestParam(name ="wages") Integer wages,
                             @RequestParam(name ="workDay") String workDay,@RequestParam(name ="formalWorkDay") String formalWorkDay,
                             @RequestParam(name ="workAge") Integer workAge,@RequestParam(name ="userStatus") Integer userStatus,
                             @RequestParam(name ="picturesUrl") String picturesUrl){
        userName = userName.trim();//去掉前后的空格

        int result = userService.creatUser( userName,userGender,userCardNum,birthday,education,birthplace,phone,userWorkNum,jobName,wages,
                                            workDay,formalWorkDay,workAge,userStatus,picturesUrl);
        return 1 == result ? "成功" : "失败";
        //http://localhost:8081/user/create?userName=小刚&userGender=1&userCardNum=370101199901018765&birthday=19990101&&education=本科&birthplace=山东青岛&phone=18765434567&userWorkNum=00000003&jobName=测试工程师&wages=8000&workDay=20220501&formalWorkDay=20220601&workAge=2&userStatus=1&picturesUrl=00000000
    }

    //修改一个员工
    @RequestMapping("/user/update")
    public String userUpdate(@RequestParam(name ="userId") BigInteger userId, @RequestParam(name ="userName") String userName,
                             @RequestParam(name ="userGender") Integer userGender, @RequestParam(name ="userCardNum") String userCardNum,
                             @RequestParam(name ="birthday") String birthday, @RequestParam(name ="education") String education,
                             @RequestParam(name ="birthplace") String birthplace, @RequestParam(name ="phone") String phone,
                             @RequestParam(name ="userWorkNum") String userWorkNum, @RequestParam(name ="jobName") String jobName,
                             @RequestParam(name ="wages") Integer wages, @RequestParam(name ="workDay") String workDay,
                             @RequestParam(name ="formalWorkDay") String formalWorkDay, @RequestParam(name ="workAge") Integer workAge,
                             @RequestParam(name ="userStatus") Integer userStatus, @RequestParam(name ="picturesUrl") String picturesUrl){

        User user = userService.getUserInfo(userId);
        if (user == null){
            return null;
        } else {
            int result = userService.updateUser(userId,userName,userGender,userCardNum,birthday,education,birthplace,phone,userWorkNum,jobName,wages,
                    workDay,formalWorkDay,workAge,userStatus,picturesUrl);
            return 1 == result ? "成功" : "失败";
        }

        //http://localhost:8081/user/update?userId=38&userName=白敬亭&userGender=1&userCardNum=370101199901018765&birthday=19990101&education=研究生&birthplace=山东济南&phone=18765434567&userWorkNum=00000007&jobName=测试工程师&wages=8000&workDay=20220501&formalWorkDay=20220601&workAge=2&userStatus=1&picturesUrl=00000000
    }

    //删除一个员工
    @RequestMapping("/user/delete")
    public String userDelete(@RequestParam(name ="userId") BigInteger userId){
        User user = userService.getUserInfo(userId);
        if (user == null){
            return null;
        } else {
            int result = userService.deleteUser(userId);
            return 1 == result ? "成功":"失败";
        }

    }
}
